package com.itboy.mall.wms.warepurchasedetail.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.itboy.common.exception.CustomException;
import com.itboy.common.utils.PageUtils;
import com.itboy.common.core.page.PagePlus;
import com.itboy.common.core.page.TableDataInfo;
import com.itboy.mall.wms.wareinfo.domain.MallWareInfo;
import com.itboy.mall.wms.wareinfo.service.IMallWareInfoService;
import com.itboy.mall.wms.warepurchase.constant.WareConstant;
import com.itboy.mall.wms.warepurchase.domain.MallWarePurchase;
import com.itboy.mall.wms.warepurchase.service.IMallWarePurchaseService;
import com.itboy.mall.wms.warepurchasedetail.bo.PurchaseDetailBo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.itboy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itboy.mall.wms.warepurchasedetail.bo.MallWarePurchaseDetailAddBo;
import com.itboy.mall.wms.warepurchasedetail.bo.MallWarePurchaseDetailQueryBo;
import com.itboy.mall.wms.warepurchasedetail.bo.MallWarePurchaseDetailEditBo;
import com.itboy.mall.wms.warepurchasedetail.domain.MallWarePurchaseDetail;
import com.itboy.mall.wms.warepurchasedetail.mapper.MallWarePurchaseDetailMapper;
import com.itboy.mall.wms.warepurchasedetail.vo.MallWarePurchaseDetailVo;
import com.itboy.mall.wms.warepurchasedetail.service.IMallWarePurchaseDetailService;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 采购需求
Service业务层处理
 *
 * @author itboy
 * @date 2021-08-10
 */
@Service
public class MallWarePurchaseDetailServiceImpl extends ServicePlusImpl<MallWarePurchaseDetailMapper, MallWarePurchaseDetail> implements IMallWarePurchaseDetailService {

	@Autowired
	private IMallWareInfoService iMallWareInfoService;

	@Autowired
	private IMallWarePurchaseDetailService iMallWarePurchaseDetailService;

	@Autowired
	private IMallWarePurchaseService iMallWarePurchaseService;
    @Override
    public MallWarePurchaseDetailVo queryById(Long id){
        return getVoById(id, MallWarePurchaseDetailVo.class);
    }

    @Override
    public TableDataInfo<MallWarePurchaseDetailVo> queryPageList(MallWarePurchaseDetailQueryBo bo) {
        PagePlus<MallWarePurchaseDetail, MallWarePurchaseDetailVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), MallWarePurchaseDetailVo.class);
		List<MallWarePurchaseDetailVo> recordsVo = result.getRecordsVo();
		if(CollectionUtil.isNotEmpty(recordsVo)){
			List<Long> wareIdList = recordsVo.stream().map(e -> e.getWareId()).collect(Collectors.toList());
			LambdaQueryWrapper<MallWareInfo> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.in(MallWareInfo::getId,wareIdList);
			List<MallWareInfo> mallWareInfoList = iMallWareInfoService.list(queryWrapper);
			if(CollectionUtil.isNotEmpty(mallWareInfoList)){
				recordsVo.forEach(e->{
					MallWareInfo mallWareInfo = mallWareInfoList.stream().filter(t -> t.getId().equals(e.getWareId())).findFirst().orElse(null);
					e.setWareName(mallWareInfo.getName());
				});
			}

		}

		result.setRecordsVo(recordsVo);
		return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<MallWarePurchaseDetailVo> queryList(MallWarePurchaseDetailQueryBo bo) {
        return listVo(buildQueryWrapper(bo), MallWarePurchaseDetailVo.class);
    }

    private LambdaQueryWrapper<MallWarePurchaseDetail> buildQueryWrapper(MallWarePurchaseDetailQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallWarePurchaseDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPurchaseId() != null, MallWarePurchaseDetail::getPurchaseId, bo.getPurchaseId());
        lqw.eq(bo.getSkuId() != null, MallWarePurchaseDetail::getSkuId, bo.getSkuId());
        lqw.eq(bo.getSkuNum() != null, MallWarePurchaseDetail::getSkuNum, bo.getSkuNum());
        lqw.eq(bo.getSkuPrice() != null, MallWarePurchaseDetail::getSkuPrice, bo.getSkuPrice());
        lqw.eq(bo.getWareId() != null, MallWarePurchaseDetail::getWareId, bo.getWareId());
        lqw.eq(bo.getStatus() != null, MallWarePurchaseDetail::getStatus, bo.getStatus());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(MallWarePurchaseDetailAddBo bo) {
        MallWarePurchaseDetail add = BeanUtil.toBean(bo, MallWarePurchaseDetail.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(MallWarePurchaseDetailEditBo bo) {
        MallWarePurchaseDetail update = BeanUtil.toBean(bo, MallWarePurchaseDetail.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(MallWarePurchaseDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

	@Override
	@Transactional
	public Boolean mergeAssignPurchaseOrder(PurchaseDetailBo bo) {
		MallWarePurchase mallWarePurchase = iMallWarePurchaseService.getById(bo.getPurchaseId());
		if(ObjectUtil.isNotNull(mallWarePurchase)){
			if(mallWarePurchase.getStatus().equals(WareConstant.PurchaseStatusEnum.RECEIVED.getCode())){
				throw new CustomException("采购单"+WareConstant.PurchaseStatusEnum.RECEIVED.getMessage());
			}else if(mallWarePurchase.getStatus().equals(WareConstant.PurchaseStatusEnum.FINISH.getCode())){
				throw new CustomException("采购单"+WareConstant.PurchaseStatusEnum.FINISH.getMessage());
			}else if(mallWarePurchase.getStatus().equals(WareConstant.PurchaseStatusEnum.HASEERROR.getCode())){
				throw new CustomException("采购单"+WareConstant.PurchaseStatusEnum.HASEERROR.getMessage());
			}else{
				List<MallWarePurchaseDetail> warePurchaseDetailList = new ArrayList<>();
				if(bo.getIds().length>0){
					List<Long> idList = Arrays.asList(bo.getIds());
					for (Long id : idList) {
						MallWarePurchaseDetail warePurchaseDetail = this.baseMapper.selectById(id);
						if(ObjectUtil.isNotNull(warePurchaseDetail)){
							if(warePurchaseDetail.getStatus().equals(WareConstant.PurchaseDetailStatusEnum.BUYING.getCode())){
								throw new CustomException(warePurchaseDetail.getId()+":需求单"+WareConstant.PurchaseDetailStatusEnum.BUYING.getMessage());
							}else if(warePurchaseDetail.getStatus().equals(WareConstant.PurchaseDetailStatusEnum.FINISH.getCode())){
								throw new CustomException(warePurchaseDetail.getId()+":需求单"+WareConstant.PurchaseDetailStatusEnum.FINISH.getMessage());
							}else if(warePurchaseDetail.getStatus().equals(WareConstant.PurchaseDetailStatusEnum.HASEERROR.getCode())){
								throw new CustomException(warePurchaseDetail.getId()+":需求单"+WareConstant.PurchaseDetailStatusEnum.HASEERROR.getMessage());
							}else{
								BeanUtils.copyProperties(bo,warePurchaseDetail);
								warePurchaseDetail.setId(id);
								warePurchaseDetail.setPurchaseId(bo.getPurchaseId());
								warePurchaseDetail.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
								warePurchaseDetailList.add(warePurchaseDetail);
							}
						}
					}
					if(CollectionUtils.isNotEmpty(warePurchaseDetailList)){
						return iMallWarePurchaseDetailService.updateBatchById(warePurchaseDetailList);
					}
				}
			}
		}
		return false;
	}
}
